<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item :label="$t('employee_name')" prop="employeeName">
        <el-input
          v-model="queryParams.employeeName"
          :placeholder="$t('please_enter', { name: $t('employee_name') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('type_of_off')" prop="dimissionType">
        <el-select
          v-model="queryParams.dimissionType"
          :placeholder="$t('please_select', { name: $t('type_of_off') })"
          clearable
        >
          <el-option
            v-for="dict in dict.type.depart_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item :label="$t('processor')" prop="transactor">
        <el-input
          v-model="queryParams.transactor"
          :placeholder="$t('please_enter', { name: $t('processor') })"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >{{ $t("search") }}</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{
          $t("reset")
        }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >{{ $t("add") }}</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dimissionList"
      @selection-change="handleSelectionChange"
      border
      :max-height="tableHeight"
    >
      <el-table-column
        :label="$t('employee_name')"
        align="center"
        prop="employeeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('department')"
        align="center"
        prop="department"
        :show-overflow-tooltip="true"
        :formatter="getDepartmentName"
      />
      <el-table-column
        :label="$t('type_of_off')"
        align="center"
        prop="dimissionType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.depart_type"
            :value="scope.row.dimissionType"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('resignation_date')"
        align="center"
        prop="dimissionDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dimissionDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('reason_for_leaving')"
        align="center"
        prop="reasonsLeaving"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('processor')"
        align="center"
        prop="transactor"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('action')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >{{ $t("modify") }}</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >{{ $t("delete") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      :page-sizes="[10,20,30,50,100, 200]"
    />

    <!-- 添加或修改离职信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item :label="$t('type_of_off')" prop="dimissionType">
          <el-radio-group v-model="form.dimissionType">
            <el-radio
              :label="dict.value"
              v-for="dict in dict.type.depart_type"
              :key="dict.value"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item
              :label="$t('select_employing_company')"
              prop="employer"
            >
              <employerSelect v-model="form.employer"></employerSelect>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.employer">
            <el-form-item :label="$('select_the_employee1')" prop="employee">
              <employeeSelect
                :employerId="form.employer"
                v-model="form.employee"
              ></employeeSelect> </el-form-item
          ></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('department')" prop="department">
              <treeselect
                v-model="form.department"
                :options="deptOptions"
                :show-count="true"
                :placeholder="$t('please_select', { name: $t('department') })"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('processor')" prop="transactor">
              <el-input
                v-model="form.transactor"
                :placeholder="$t('please_enter', { name: $t('processor') })"
                :show-word-limit="true"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-form-item :label="$t(reason_for_leaving)" prop="reasonsLeaving">
          <el-input
            v-model="form.reasonsLeaving"
            :placeholder="
              $t('please_enter', { name: $t('reason_for_leaving') })
            "
            :show-word-limit="true"
            type="textarea"
            :rows="4"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="cancel">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDimission,
  getDimission,
  delDimission,
  addDimission,
  updateDimission,
} from "@/api/custom/employeeDepart";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import employerSelect from "@/views/components/custom/employerSelect.vue";
import employeeSelect from "@/views/components/custom/employeeSelect.vue";
export default {
  name: "Dimission",
  dicts: ["depart_type"],
  components: { Treeselect, employerSelect, employeeSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 离职信息表格数据
      dimissionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dimissionType: null,
        employeeName: null,
        transactor: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 部门树选项
      deptOptions: undefined,
    };
  },
  created() {
    this.getDeptTree();
    this.getList();
  },
  methods: {
    /** 查询离职信息列表 */
    getList() {
      this.loading = true;
      listDimission(this.queryParams).then((response) => {
        this.dimissionList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    findItemById(list, id) {
      if (!list || !Array.isArray(list) || list.length === 0) {
        return null;
      }
      for (let item of list) {
        if (item.id == id) {
          return item;
        } else {
          const t = this.findItemById(item.children, id);
          if (t) {
            return t;
          }
        }
      }
      return null;
    },
    getDepartmentName(row) {
      console.log(row);
      const item = this.findItemById(this.deptOptions, row.department);
      return item ? item.label : "-";
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dimissionType: null,
        dimissionDate: null,
        reasonsLeaving: null,
        employer: null,
        employee: null,
        department: null,
        transactor: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t("add");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDimission(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t("modify");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDimission(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          } else {
            addDimission(this.form).then((response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm(this.$t("delete_data"))
        .then(function () {
          return delDimission(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess(this.$t("operation_successful"));
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dimission/export",
        {
          ...this.queryParams,
        },
        `dimission_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
